package org.gary.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.gary.base.model.Category;
import org.gary.base.model.Link;
import org.gary.base.model.Online;
import org.gary.base.service.CategoryService;
import org.gary.base.service.LinkService;
import org.gary.base.service.OnlineService;
import org.gary.core.service.InitService;
import org.gary.news.model.News;
import org.gary.news.service.NewsService;
import org.springframework.stereotype.Component;
@Component("initService")
public class InitServiceImpl implements InitService {

	private CategoryService categoryService;
	private NewsService newsService;
	private LinkService linkService;
	private OnlineService onlineService;

	@Override
	public void newsMinArticle(Map<String, Object> session) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("entity.lang =", session.get("WW_TRANS_I18N_LOCALE").toString());
		List<News> news=newsService.getNews(where, 1, 10, new Object[]{" entity.id desc "});
		session.put("minnews", news);
	}

	@Override
	public void newsNavigation(Map<String, Object> session) {
		List<Category> categorys= categoryService.getList("news",session.get("WW_TRANS_I18N_LOCALE"));
		session.put("newsCategory", categorys);
	}

	@Override
	public void minLinks(Map<String, Object> session) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("entity.lang =", session.get("WW_TRANS_I18N_LOCALE").toString());
		List<Link> links=linkService.getLink(1, 6, new Object[]{" entity.id desc "});
		session.put("links", links);
	}

	@Override
	public void minQQ(Map<String, Object> session) {
		List<Online> onlines=onlineService.getOnline();
		session.put("onlines", onlines);
	}
	
	@Override
	public void equipmentNavigation(Map<String, Object> session) {
		List<Category> categorys=categoryService.getList("equipment",session.get("WW_TRANS_I18N_LOCALE"));
		session.put("equipmentCategory", categorys);	
	}

	@Override
	public void productNavigation(Map<String, Object> session) {
		List<Category> categorys=categoryService.getList("product",session.get("WW_TRANS_I18N_LOCALE"));
		session.put("productCategory", categorys);	
	}	
	
	@Override
	public void initAll(Map<String, Object> session) {
		if(session.get("minnews")==null){
			newsMinArticle(session);
		}
		if(session.get("newsCategory")==null){
			newsNavigation(session);
		}
		if(session.get("links")==null){
			minLinks(session);
		}
		if(session.get("equipmentCategory")==null){
			equipmentNavigation(session);
		}
		if(session.get("productCategory")==null){
			productNavigation(session);
		}
		if(session.get("onlines")==null){
			minQQ(session);
		}
	}
	
	@Override
	public void updateNavigation(Map<String, Object> session) {
		newsNavigation(session);
		equipmentNavigation(session);
		productNavigation(session);
	}
	
	@Override
	public void initForceAll(Map<String, Object> session) {
		newsMinArticle(session);
		newsNavigation(session);
		minLinks(session);
		equipmentNavigation(session);
		productNavigation(session);
		minQQ(session);
	}
	
	@Resource
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@Resource
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	@Resource
	public void setLinkService(LinkService linkService) {
		this.linkService = linkService;
	}

	@Resource
	public void setOnlineService(OnlineService onlineService) {
		this.onlineService = onlineService;
	}	
}
